class Solution(object):
    def maxAlternatingSum(self, nums):
        n = len(nums)
        left = 0
        right = 1
        ans = 0
        while right < n:
            if nums[right] <= nums[right - 1]:
                right += 1
            else:
                if left < right - 1:
                    ans += nums[left] - nums[right - 1]
                left = right
                right += 1
        ans += nums[left]
        return ans

